﻿// -----------------------------------------------------------------------
// <copyright file="BinaryWriterStream.cs" company="none">
// Copyright (c) 2012 M. Alberti, xanatos(at)live.it
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
// </copyright>
// -----------------------------------------------------------------------

namespace FluentSerializer.Utilities
{
    using System;
    using System.ComponentModel;
    using System.IO;

    /// <summary>
    /// A BinaryWriter with a stream interface. It has a "priviledged" connection
    /// with <seealso cref="MemoryBuffer">MemoryBuffer</seealso>
    /// </summary>
    public class BinaryWriterStream : Stream
    {
        /// <summary>
        /// 
        /// </summary>
        private int position;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        public BinaryWriterStream(MemoryBuffer buffer)
        {
            this.Buffer = buffer;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        public BinaryWriterStream(Stream stream)
        {
            this.BinaryStream = new BinaryWriter(stream);
        }

        /// <summary>
        /// 
        /// </summary>
        public MemoryBuffer Buffer { get; protected set; }

        /// <summary>
        /// 
        /// </summary>
        public BinaryWriter BinaryStream { get; protected set; }

        /// <summary>
        /// 
        /// </summary>
        public Stream Stream
        {
            get
            {
                return this.BinaryStream != null ? this.BinaryStream.BaseStream : null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override long Position
        {
            get
            {
                if (this.BinaryStream == null)
                {
                    return this.position;
                }

                return this.Stream.Position;
            }

            set
            {
                if (this.BinaryStream == null)
                {
                    if (value < 0 || value > this.Buffer.Length)
                    {
                        throw new ArgumentOutOfRangeException("value");
                    }

                    this.position = checked((int)value);
                }
                else
                {
                    this.Stream.Position = value;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override long Length
        {
            get
            {
                if (this.BinaryStream == null)
                {
                    return this.Buffer.Length;
                }

                return this.Stream.Length;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override bool CanRead
        {
            get { return false; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override bool CanSeek
        {
            get
            {
                if (this.BinaryStream == null)
                {
                    return true;
                }

                return this.Stream.CanSeek;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override bool CanWrite
        {
            get
            {
                if (this.BinaryStream == null)
                {
                    return true;
                }

                return this.Stream.CanWrite;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void Flush()
        {
            if (this.BinaryStream != null)
            {
                this.Stream.Flush();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="origin"></param>
        /// <returns></returns>
        public override long Seek(long offset, SeekOrigin origin)
        {
            if (this.BinaryStream != null)
            {
                return this.Stream.Seek(offset, origin);
            }

            switch (origin)
            {
                case SeekOrigin.Begin:
                    this.Position = offset;
                    break;

                case SeekOrigin.Current:
                    this.Position += offset;
                    break;

                case SeekOrigin.End:
                    this.Position = this.Buffer.Length - offset;
                    break;

                default:
                    throw new ArgumentException("origin");
            }

            return this.Position;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(sbyte value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(sbyte);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override void WriteByte(byte value)
        {
            this.Write(value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(byte value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(byte);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(bool value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(bool);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(short value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(short);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(ushort value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(ushort);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(char value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(char);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(int value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(int);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(uint value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(uint);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(long value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(long);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(ulong value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(ulong);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(float value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(float);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(double value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += sizeof(double);
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(IntPtr value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += IntPtr.Size;
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void Write(UIntPtr value)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, value);
                this.position += UIntPtr.Size;
            }
            else
            {
                this.BinaryStream.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(sbyte[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(sbyte);
            }
            else
            {
                this.BinaryStream.Write((byte[])(Array)buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(byte);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(bool[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(bool);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(short[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(short);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(ushort[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(ushort);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(char[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(char);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(int[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(int);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(uint[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(uint);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(long[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(long);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(ulong[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(ulong);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(float[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(float);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(double[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * sizeof(double);
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(IntPtr[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * IntPtr.Size;
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(UIntPtr[] buffer, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, offset, count);
                this.position += count * IntPtr.Size;
            }
            else
            {
                this.BinaryStream.Write(buffer, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="sizeElement"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public void Write(Array buffer, int sizeElement, int offset, int count)
        {
            if (this.BinaryStream == null)
            {
                this.Buffer.Write(this.position, buffer, sizeElement, offset, count);
                this.position += count * sizeElement;
            }
            else
            {
                this.BinaryStream.Write(buffer, sizeElement, offset, count);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            this.Buffer = null;
            this.BinaryStream = null;
        }
    }
}